package com.asha.vrlib.strategy.interactive;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.Log;

import com.asha.vrlib.MD360Director;

import java.util.List;

/**
 * Created by hzqiujiadi on 16/3/19.
 * hzqiujiadi ashqalcn@gmail.com
 */
public class TouchStrategy extends AbsInteractiveStrategy {

    private static final float sDensity =  Resources.getSystem().getDisplayMetrics().density;

    private static float sDamping = 0.2f;

    private static final String TAG = "TouchStrategy";
    private int mHandleDirectorIndex = -1;

    public TouchStrategy(InteractiveModeManager.Params params) {
        super(params);
    }

    private static int correctDirectorIndex(int size, int index) {
        if (size == 2) {
            return size - index - 1;
        } else if (size == 4) {
            if (index == 0) {
                return 2;
            } else if (index == 1) {
                return 3;
            } else if (index == 2) {
                return 0;
//            } else if (index == 3){
//                return 2;
            } else {
                return 1;
            }
        } else {
            return index;
        }
    }

    private static boolean isPointInRect(Rect rect, Point test) {
        if (test.x >= (float) rect.left && test.x <= (float) rect.right
                && test.y >= (float) rect.top && test.y <= (float) rect.bottom) {
            return true;
        }
        return false;
    }
    @Override
    public void onResume(Context context) {}

    @Override
    public void onPause(Context context) {}

    @Override
    public boolean handleDown(int x, int y) {
        List<Point> poses = getDisplayPoss();
        List<Point> sizes = getDisplaySizes();
        int count = poses.size();
        Rect rect = new Rect();
        for (int index = 0; index < count; index++) {
            rect.left = poses.get(index).x;
            rect.top = poses.get(index).y;
            rect.right = rect.left + sizes.get(index).x;
            rect.bottom = rect.top + sizes.get(index).y;
            if (isPointInRect(rect, new Point(x, y))) {
                mHandleDirectorIndex = correctDirectorIndex(count, index);
                return true;
            }
        }
        return false;
    }

    @Override
    public void updateSensitivity(float sens) {
        sDamping = sens;
    }

    @Override
    public boolean handleDrag(final int distanceX, final int distanceY) {
        List<MD360Director> directors = getDirectorList();
        if (directors != null && mHandleDirectorIndex != -1 && directors.size() > mHandleDirectorIndex) {
            MD360Director director = directors.get(mHandleDirectorIndex);
            director.setDeltaX(director.getDeltaX() - distanceX / sDensity * sDamping);
            director.setDeltaY(director.getDeltaY() - distanceY / sDensity * sDamping);
        }
//        for (MD360Director director : getDirectorList()){
//            director.setDeltaX(director.getDeltaX() - distanceX / sDensity * sDamping);
//            director.setDeltaY(director.getDeltaY() - distanceY / sDensity * sDamping);
//        }
        return false;
    }

    @Override
    public void onOrientationChanged(Context context) {

    }

    @Override
    public void turnOnInGL(Context context) {
//        for (MD360Director director : getDirectorList()) {
//            director.reset();
//        }
    }

    @Override
    public void turnOffInGL(Context context) {
    }

    @Override
    public boolean isSupport(Context context) {
        return true;
    }
}
